const app = getApp()
import {imageUrl} from "../../utils/imageutil.js"
import {formatTime} from '../../utils/util.js'
import {provinces} from '../../utils/provinces.js'
Page({
  data: {
    imageUrl:imageUrl,
    typescurr:-1,
    provinces:provinces,
    types:[{
      title:'是否住家',
      id:1
    },{
      title:'籍贯',
      id:2
    },{
      title:'年龄',
      id:3
    },{
      title:'筛选',
      id:4
    }],
    showTime:false,
    sortcurr:1,
    housekeeperListPage:1,
    housekeeperListby:1,
    housekeeperListFlag:true,
    housekeeperList:[],
    age:'',
    ageIndex:-1,
    province:'',
    provinceIndex:-1,
    calendarHeight:0,
    calendarHeightAndBtnH:0,
    home:'',
    homeIndex:-1,
    homeList:[{
      title:'住家',
      home:'1',
      id:1
    },{
      title:'白班',
      home:'2',
      id:2
    },{
      title:'钟点工',
      home:'3',
      id:3
    }],
    agelist:[{
      title:'不限',
      age:'',
      id:1
    },{
      title:'35岁以下',
      age:'1,35',
      id:2
    },{
      title:'36-40岁',
      age:'36,40',
      id:3
    },{
      title:'41-45岁',
      age:'41,45',
      id:4
    },{
      title:'46-50岁',
      age:'46,50',
      id:5
    },{
      title:'50岁以上',
      age:'51',
      id:6
    }],
  },
  onLoad: function (options) {
    this.gethousekeeperList();
    this.setData({
      calendarHeight:app.globalData.windowHeight - ((80+132)/750*app.globalData.windowWidth),
      calendarHeightAndBtnH:app.globalData.windowHeight - (80/750*app.globalData.windowWidth),
    })
  },
  gettime(){
    this.setData({
      typescurr:-1
    })
  },
  showTimeScreening(){
    this.setData({
      showTime:true
    })
  },
  checkhome(e){
    this.removehome()
    let value = 'homeList['+e.currentTarget.dataset.index+'].check'
    this.setData({
      [value]:!this.data.homeList[e.currentTarget.dataset.index].check,
      homeIndex:e.currentTarget.dataset.index
    })
  },
  checkprovinces(e){
    this.removeprovinces()
    let value = 'provinces['+e.currentTarget.dataset.index+'].check'
    this.setData({
      [value]:!this.data.provinces[e.currentTarget.dataset.index].check,
      provinceIndex:e.currentTarget.dataset.index
    })
  },
  checkage(e){
    this.removeage()
    let value = 'agelist['+e.currentTarget.dataset.index+'].check'
    this.setData({
      [value]:!this.data.agelist[e.currentTarget.dataset.index].check,
      ageIndex:e.currentTarget.dataset.index,
    })
  },
  gethome(){
    this.setData({
      housekeeperListPage:1,
      housekeeperListFlag:true,
      housekeeperList:[]
    })
    if(this.data.homeIndex==-1){
      this.setData({
        home:''
      })
    }else{
      this.setData({
        home:this.data.homeList[this.data.homeIndex].home
      })
    }
    this.gethousekeeperList()
  },
  getprovince(){
    this.setData({
      housekeeperListPage:1,
      housekeeperListby:1,
      housekeeperListFlag:true,
      housekeeperList:[]
    })
    if(this.data.provinceIndex==-1){
      this.setData({
        province:''
      })
    }else{
      this.setData({
        province:this.data.provinces[this.data.provinceIndex].title
      })
    }
    this.gethousekeeperList()
  },
  getage(){
    this.setData({
      housekeeperListPage:1,
      housekeeperListFlag:true,
      housekeeperList:[]
    })
    if(this.data.ageIndex==-1){
      this.setData({
        age:''
      })
    }else{
      this.setData({
        age:this.data.agelist[this.data.ageIndex].age
      })
    }
    this.gethousekeeperList()
  },
  getall(){
    this.setData({
      housekeeperListPage:1,
      housekeeperListFlag:true,
      housekeeperList:[]
    })
    if(this.data.homeIndex==-1){
      this.setData({
        home:''
      })
    }else{
      this.setData({
        home:this.data.homeList[this.data.homeIndex].home
      })
    }
    if(this.data.provinceIndex==-1){
      this.setData({
        province:''
      })
    }else{
      this.setData({
        province:this.data.provinces[this.data.provinceIndex].title
      })
    }
    if(this.data.ageIndex==-1){
      this.setData({
        age:''
      })
    }else{
      this.setData({
        age:this.data.agelist[this.data.ageIndex].age
      })
    }
    this.gethousekeeperList()
  },
  removehome(){
    this.data.homeList.forEach((v,i)=>{
      let value = 'homeList['+i+'].check';
      this.setData({
        [value]:false,
        homeIndex:-1,
      })
    })
  },
  removeprovinces(){
    this.data.provinces.forEach((v,i)=>{
      let value = 'provinces['+i+'].check';
      this.setData({
        [value]:false,
        provinceIndex:-1,
      })
    })
  },
  removeage(){
    this.data.agelist.forEach((v,i)=>{
      let value = 'agelist['+i+'].check';
      this.setData({
        [value]:false,
        ageIndex:-1
      })
    })
  },
  removeall(){
    this.removehome();
    this.removeprovinces();
    this.removeage();
  },
  calendarSelect(e){
    console.log(formatTime(e.detail))
  },
  onReachBottom(){
    this.gethousekeeperList()
  },
  gethousekeeperList(){
    app.globalData.post({
      data:{
        do: 'housekeeperList',
        uid:wx.getStorageSync('userInfo').id,
        page:this.data.housekeeperListPage,
        by:this.data.housekeeperListby,
        age:this.data.age,
        province:this.data.province,
        home:this.data.home,
        pagesize:10,
        type:4
      }
    }).then(res=>{
      wx.stopPullDownRefresh();
      this.setData({
        typescurr:-1
      })
      if(res.data.data.length==0){
        this.setData({
          housekeeperListFlag:false
        })
      }else{
        let arr = this.data.housekeeperList.concat(res.data.data);
        this.setData({
          housekeeperList:arr,
          housekeeperListPage:this.data.housekeeperListPage+1
        })
      }
    })
  },
  changetypescurr(e){
    this.setData({
      typescurr:this.data.typescurr==e.currentTarget.dataset.id?-1:e.currentTarget.dataset.id
    })
  },
  sortchange(e){
    this.setData({
      sortcurr:e.currentTarget.dataset.id
    })
    if(this.data.sortcurr==this.data.housekeeperListby){

    }else{
      this.setData({
        housekeeperListPage:1,
        housekeeperListby:this.data.sortcurr,
        housekeeperListFlag:true,
        housekeeperList:[],
      })
      this.gethousekeeperList();
    }
  },
  changebtn(){
    wx.navigateTo({
      url: './candidate?type=3',
    })
  },
  to_aunt(e){
    wx.navigateTo({
      url: '../aunt/index?uid='+e.currentTarget.dataset.id+'&type=3',
    })
  },
})